package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Autowired
    private WmNewsService wmNewsService;
    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    @Override
    public ResponseResult getlist(ChannelDto channelDto) {

        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        channelDto.checkParam();
        Page<WmChannel> page = new Page<WmChannel>(channelDto.getPage(),channelDto.getSize());
        String name = channelDto.getName();
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmChannel::getCreatedTime);
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.eq(WmChannel::getName, name);
        }
        Integer status = channelDto.getStatus();
        if (status != null) {
            queryWrapper.eq(WmChannel::getStatus, status);
        }
        this.page(page, queryWrapper);
        PageResponseResult result = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }


    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        String name = wmChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel one = this.getOne(new LambdaQueryWrapper<WmChannel>().eq(WmChannel::getName, name));
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已存在，不能重复添加！");
        }
        wmChannel.setIsDefault(true);
        wmChannel.setCreatedTime(new Date());
        this.save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        WmChannel wmChannel = this.getById(id);
        Boolean status = wmChannel.getStatus();
        if (BooleanUtils.isTrue(status)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "需要禁用才能删除!");
        }
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        String name = wmChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }
        int count1 = this.count(new LambdaQueryWrapper<WmChannel>().eq(WmChannel::getName, name));
        if (count1 > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已存在，不能重复添加！");
        }

        Boolean status = wmChannel.getStatus();
        if (BooleanUtils.isFalse(status)) {
            //禁用则需要查询是否有素材关联该频道
            int count = wmNewsService.count(new LambdaQueryWrapper<WmNews>().eq(WmNews::getChannelId, wmChannel.getId()));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "有素材关联该频道，修改失败！");
            }
        }
        this.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
